package com.seecoder.BlueWhale.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

@Component
public class JsonUtil {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static String object2Json(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException("Failed to convert object to JSON string");
        }
    }

    public static String json2String(String json, String key) {
        try {
            return objectMapper.readTree(json).get(key).asText();
        } catch (Exception e) {
            throw new RuntimeException("Failed to convert JSON string to string");
        }
    }
}
